<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
    $orderby='paixu desc';
	$total='';	
	if(!empty($_RQ['storeid'])){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if(!empty($_RQ['goodsid'])){
		$params['goodsid']=intval($_RQ['goodsid']);
		$return['params']['goodsid']=intval($_RQ['goodsid']);
	}
	$return['list']=pdo_getslice('jy_weishop_store_goods',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	if(empty($return['list'])){
		$this->model('store')->upgoods();
	}
	foreach($return['list'] as &$v){
		$v['store']=pdo_getcolumn('jy_weishop_store',array('id'=>$v['storeid']),'title');
		$goods=pdo_get('jy_weishop_goods',array('id'=>$v['goodsid']),array('title','total','price','image','salenum'));
		$v['isoptions']=pdo_count('jy_weishop_goods_option',array('goodsid'=>$v['goodsid']),'id');
		$v['salenum']=$goods['salenum'];
		$v['oldtotal']=$goods['total'];
		$v['oldprice']=$goods['price'];
		$v['title']=$goods['title'];
		$v['image']=$goods['image'];
	}
	paixu($return['list'],'salenum','desc');
	$htmls=array(
		'type'=>'list',	
		'tabs'=>array(
			array('op'=>'list','title'=>'库存管理'),
		),
		'list'=>array('data'=>'list','tip'=>'一键同步：会把所有商品加入门店库存（新加商品也会同步上去）；一键重置：会重置所有门店库存的设置，恢复跟平台一直！;规格的库存需单独设置！','del_one'=>false,'delall'=>false,'edit'=>false,'list'=>array(
			array('name'=>'goodsid','title'=>'商品ID','type'=>'string'),
			array('name'=>'title','title'=>'商品名称','type'=>'string'),
			array('name'=>'salenum','title'=>'总销量','type'=>'string'),
			array('name'=>'image','title'=>'商品图片','type'=>'image'),
			array('name'=>'store','title'=>'所属门店','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'oldprice','title'=>'平台价格','type'=>'string'),
			array('name'=>'price','title'=>'本店价格(点击编辑↓)','type'=>'string','set'=>'priceset'),
			array('name'=>'oldtotal','title'=>'平台库存','type'=>'string'),
			array('name'=>'total','title'=>'本店库存(点击编辑↓)','type'=>'string','set'=>'set'),		
		),'action'=>array(
			array('type'=>'href','op'=>'options','title'=>'设置规格','show'=>array('isoptions >'=>0)),
		),'batch'=>array(
			array('type'=>'post','url'=>'jy_weishop/store.totalset/upgoods/','msg'=>'确定一键同步商品？把新商品加入库存列表','title'=>'一键同步商品'),
			array('type'=>'post','url'=>'jy_weishop/store.totalset/conggoods/','msg'=>'确定一键重置商品？所有库存设置将失效，重置成为平台商品的库存！','title'=>'一键重置商品'),
		)),
		'search'=>array(			
			array('name'=>'storeid','width'=>15,'title'=>'选择门店','type'=>'select','options'=>'store'),
			array('name'=>'goodsid','width'=>15,'title'=>'选择商品','type'=>'choose','action'=>'jy_weishop/goods.get_goods//nomerch=true',),
		)
	);
	$return['status']=array('0'=>'下架','1'=>'上架','2'=>'售罄','3'=>'回收站');
	$return['store']=pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['htmls']=$htmls;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	exi($return);
}
if($_RQ['op']=='options'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择商品！','error');
	}
	$goodsid=pdo_getcolumn('jy_weishop_store_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),'goodsid');
	$storeid=pdo_getcolumn('jy_weishop_store_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),'storeid');
	$options=$this->pdo_getall('goods_option',array('goodsid'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
	foreach($options as &$ov){
		$isset=$this->pdo_getcolumn('store_goods_option',array('optionid'=>$ov['id'],'goodsid'=>$goodsid,'storeid'=>$storeid,'uniacid'=>$_SESSION['uniacid']),'id');
		if(!$isset){
			$data=array(
				'optionid'=>$ov['id'],
				'goodsid'=>$goodsid,
				'storeid'=>$storeid,
				'stock'=>$ov['stock'],
				'uniacid'=>$_SESSION['uniacid']
			);
			pdo_insert('jy_weishop_store_goods_option',$data);
		}
	}
	$list=$this->pdo_getall('store_goods_option',array('goodsid'=>$goodsid,'storeid'=>$storeid,'uniacid'=>$_SESSION['uniacid']));
	foreach($list as &$v){
			$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$v['optionid']),'specs');
			if(!$specs){
				pdo_delete('jy_weishop_store_goods_option',array('id'=>$v['id'],'uniacid'=>$_SESSION['uniacid']));
				continue;
			}
			$specs=explode("_",$specs);
			foreach($specs as &$spec){
					$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
			}
			$v['selespename']=implode("，",$specs);
			$v['oldstock']=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$v['optionid']),'stock');
			$v['oldprice']=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$v['optionid']),'price');
	}
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回库存管理'),
		),
		'list'=>array('data'=>'list','tip'=>'这里设置已有的规格，只能设置库存','del_one'=>false,'delall'=>false,'edit'=>false,'list'=>array(
			array('name'=>'selespename','title'=>'规格名称','type'=>'string'),
			array('name'=>'oldprice','title'=>'平台价格','type'=>'string'),
			array('name'=>'price','title'=>'本店价格(点击编辑↓)','type'=>'string','set'=>'optionprice'),
			array('name'=>'oldstock','title'=>'平台库存','type'=>'string'),
			array('name'=>'stock','title'=>'本店库存(点击编辑↓)','type'=>'string','set'=>'optionset'),		
		)),
	);
	$return['htmls']=$htmls;
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='upgoods'){
	$this->model('store')->upgoods();
	exi('同步商品成功！');
}
if($_RQ['op']=='conggoods'){
	$this->model('store')->upgoods(true);
	exi('重置商品成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择商品！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['total'])){
		$update['total']=$post['total'];
	}
	if($update){
		$this->pdo_update('store_goods',$update,array('id'=>$id));
	}
	exi('修改成功');
}
if($_RQ['op']=='priceset'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择商品！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['price'])){
		$update['price']=$post['price'];
	}
	if($update){
		$this->pdo_update('store_goods',$update,array('id'=>$id));
	}
	exi('修改成功');
}
if($_RQ['op']=='optionset'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择规格！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['stock'])){
		$update['stock']=$post['stock'];
	}
	if($update){
		$this->pdo_update('store_goods_option',$update,array('id'=>$id));
	}
	exi('修改成功');
}
if($_RQ['op']=='optionprice'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择规格！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['price'])){
		$update['price']=$post['price'];
	}
	if($update){
		$this->pdo_update('store_goods_option',$update,array('id'=>$id));
	}
	exi('修改成功');
}